<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-09-29
 * Time: 14:29
 */

namespace app\helper;


use app\constant\ActivesConst;
use app\model\AdminActives;
use app\queue\QueuePub;
use app\queue\QueueTypes;
use DateTime;

class ActiveHelper
{

    public static function save($postData)
    {

        //VIP会员商品不需要校验状态
        if ($postData['type'] != ActivesConst::ACTIVE_TYPE_VIP) {
            //检测活动时间是否正确设置
            $checkResult = ActiveHelper::checkDataTime($postData);
            if (!$checkResult['done']) {
                return ['done' => false, 'msg' => '操作失败' . $checkResult['msg']];
            }

            //如果不显示就一直是保存状态
            if ($postData['show'] == ActivesConst::SHOW_NO) {
                $postData['status'] = ActivesConst::STATUS_SAVED;
            } else {
                //否则根据时间来处理当前对应的状态
                $postData = ActiveHelper::changeStatus($postData);
            }
        }

        if (isset($postData['id'])) {
            $result = AdminActives::doSave($postData['id'], $postData);
        } else {
            $result = AdminActives::add($postData);
            $postData = $result;
        }
        //发布消息队列
        if ($postData['show'] == ActivesConst::SHOW_YES) {
            ActiveHelper::pubQueue($postData);
            ActiveHelper::pubUseEndQueue($postData);
        }
        if ($result) {
            return ['done' => true, 'msg' => '操作成功'];
        } else {
            return ['done' => false, 'msg' => '操作失败'];
        }
    }

    /**
     * @param $data {Object} 活动的对象
     * @return mixed {Object} 处理好状态的活动对象
     *
     * @throws
     */
    public static function changeStatus($data)
    {
        $endTime = $data['end_time'];
        $startTime = $data['start_time'];
        $startDate = new Datetime($startTime);
        $endDate = new Datetime($endTime);
        $cDate = new DateTime();
        //判断是否已经开始
        if ($cDate->getTimestamp() < $startDate->getTimestamp()) {
            //未开始了
            $data['status'] = ActivesConst::STATUS_WAIT;
            //判断是否结束
        } else if ($cDate->getTimestamp() < $endDate->getTimestamp()) {
            //已经开始但未结束
            $data['status'] = ActivesConst::STATUS_NORMAL;
        } else {
            //结束了
            $data['status'] = ActivesConst::STATUS_END;
        }
        return $data;
    }

    /**
     * 检测使用日期是否晚于截止日期，并且晚于当前
     */
    public static function checkDataTime($data)
    {
        $cDate = new DateTime();
        $endTime = $data['end_time'];
        $endUseTime = $data['end_use_time'];
        $endUseDate = new Datetime($endUseTime);
        $endDate = new Datetime($endTime);
        if ($endDate->getTimestamp() < $cDate->getTimestamp()) {
            return ['done' => false, 'msg' => '结束时间不得早于当前时间'];
        } else if ($endUseDate->getTimestamp() < $endDate->getTimestamp()) {
            return ['done' => false, 'msg' => '使用截止时间不得早于活动结束时间'];
        }
        return ['done' => true, 'msg' => null];
    }

    /**
     * 发布活动使用截止时间消息
     * @param $data {Object} 活动的对象
     * @return bool
     */
    public static function pubUseEndQueue($data)
    {
        $id = $data['id'];
        $endUseTime = $data['end_use_time'];
        $endDate = new \Datetime($endUseTime);
        $cDate = new \DateTime();

        $delay = -1;
        if ($cDate->getTimestamp() < $endDate->getTimestamp()) {
            $delay = $endDate->getTimestamp() - $cDate->getTimestamp();
        }
        //如果没有当前活动相关的消息队列的话，就发布
        if ($delay != -1) {
            //发布延时队列
            $isPushed = QueuePub::later($delay, QueueTypes::ACTIVES_USE_END_TIME, ['id' => $id]);
            return $isPushed;
        }
        return false;
    }

    /**
     * 发布消息
     * @param $data {Object} 活动的对象
     * @return bool
     */
    public static function pubQueue($data)
    {

        //如果是已经结束的活动，就不需要再发布消息了
        if ($data['status'] == ActivesConst::STATUS_END) {
            return false;
        }

        $id = $data['id'];
        $endTime = $data['end_time'];
        $startTime = $data['start_time'];
        $startDate = new \Datetime($startTime);
        $endDate = new \Datetime($endTime);
        $cDate = new \DateTime();

        $delay = -1;
        //判断是否已经开始
        if ($cDate->getTimestamp() < $startDate->getTimestamp()) {
            //未开始了
            $delay = $startDate->getTimestamp() - $cDate->getTimestamp();
            //判断是否结束
        } else if ($cDate->getTimestamp() < $endDate->getTimestamp()) {
            $delay = $endDate->getTimestamp() - $cDate->getTimestamp();
            //已经开始但未结束
            $data['status'] = ActivesConst::STATUS_NORMAL;
        } else {
            //结束了
            $data['status'] = ActivesConst::STATUS_END;
        }
        //如果没有当前活动相关的消息队列的话，就发布
        if ($delay != -1) {
            //发布延时队列
            $isPushed = QueuePub::later($delay, QueueTypes::ACTIVES_STATUS_CHECK, ['id' => $id]);
            return $isPushed;
        }
        return false;
    }


}